﻿using CL.Tools.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CL.Mobile.OnlinePay
{
    public partial class PayResult : System.Web.UI.Page
    {
        //查询接口
        private string url_doquery = "https://r630.speedpos.cn:17630/urlapi/gateway/doquery";

        static string key = "83c081bb7e44a4d2c6c3ce772b94452e"; //密钥
        static string mch_id = "103520086863";//商户ID;

        string orderno = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //生成订单
                orderno = Request.QueryString["orderno"];
                ltOrderNo.Text = orderno;
            }
        }

        #region 查询
        /// <summary>
        /// 构造查询参数
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private SortedDictionary<string, string> buildDoQuery(string key)
        {
            SortedDictionary<String, String> map = new SortedDictionary<String, String>();
            map.Add("mch_id", mch_id);
            map.Add("nonce_str", DateTime.Now.ToString("yyyyMMddHHmmss"));
            map.Add("out_trade_no", ltOrderNo.Text);
            //map.Add("transaction_id", ltAmount.Text); //第三方订单号
            map.Add("sign", buildSign(map, key));
            return map;
        }
        #endregion

        /// <summary>
        /// 构造签名
        /// </summary>
        /// <param name="map"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private string buildSign(SortedDictionary<string, string> map, string key)
        {
            string sign = string.Empty;

            foreach (KeyValuePair<string, string> item in map)
            {
                sign += string.Format("{0}={1}&", item.Key, item.Value);
            }
            if (sign.Length > 0) sign = string.Format("{0}key={1}", sign, key);
            string encryptSign = Utils.MD5(sign);
            return encryptSign;
        }

        protected void btnOrderQuery_Click(object sender, EventArgs e)
        {
            //提交查询
            var postData = buildDoQuery(key);
            string resData = Utils.HttpSubmit(url_doquery, postData);
            ltQueryResult.Text = resData;
        }
    }

}